Imports System.IO
Imports System.Windows.Forms
Imports Vintasoft.Pdf
Imports Vintasoft.Pdf.Tree

Namespace Pdf
	Public Partial Class EmbeddedFilesForm
		Inherits Form

		#Region "Fields"

		Const DateTimeFormat As String = "dd.MM.yyyy hh:mm:ss"
		Const FormTitle As String = "Embedded Files"
		Const AddEmbeddedFileMessage As String = "Add embedded file '{0}'..."

		Private _showingFileProperties As Boolean


		#End Region



		#Region "Constructors"

		Public Sub New()
			InitializeComponent()
			Text = FormTitle
			compressionComboBox.Items.Add(PdfCompression.None)
			compressionComboBox.Items.Add(PdfCompression.Zip)
			compressionComboBox.Items.Add(PdfCompression.Lzw)
			compressionComboBox.Items.Add(PdfCompression.RunLength)
		End Sub

		#End Region



		#Region "Properties"

		Public Property CanEditEmbeddedFiles() As Boolean
			Get
				Return desriptionTextBox.Enabled
			End Get
			Set
				desriptionTextBox.Enabled = value
				removeButton.Visible = value
				addButton.Visible = value
				compressionComboBox.Enabled = value
			End Set
		End Property

		Private _document As PdfDocument
		Public Property Document() As PdfDocument
			Get
				Return _document
			End Get
			Set
				_document = value
				embeddedFilesDataGridView.Rows.Clear()
				If _document.EmbeddedFiles IsNot Nothing Then
					For Each name As String In _document.EmbeddedFiles.Keys
						AddRow(name, _document.EmbeddedFiles(name))
					Next
				End If
			End Set
		End Property

		Private ReadOnly Property SelectedFileSpecification() As PdfEmbeddedFileSpecification
			Get
				If embeddedFilesDataGridView.SelectedRows.Count > 0 Then
					Dim name As String = DirectCast(embeddedFilesDataGridView.SelectedRows(0).Tag, String)
					Return Document.EmbeddedFiles(name)
				End If
				Return Nothing
			End Get
		End Property


		#End Region



		#Region "Methods"

		Private Function AddRow(name As String, fileSpecification As PdfEmbeddedFileSpecification) As DataGridViewRow
			Dim row As New DataGridViewRow()
			row.Tag = name
			row.Cells.Add(New DataGridViewTextBoxCell())
			row.Cells.Add(New DataGridViewTextBoxCell())
			row.Cells.Add(New DataGridViewTextBoxCell())
			UpdateRowInformation(row)
			embeddedFilesDataGridView.Rows.Add(row)
			Return row
		End Function

		Private Sub UpdateRowInformation(row As DataGridViewRow)
			Dim fileSpecification As PdfEmbeddedFileSpecification = Document.EmbeddedFiles(DirectCast(row.Tag, String))
			row.Cells(0).Value = fileSpecification.Filename
			Dim compressedSize As Integer = fileSpecification.EmbeddedFile.Length
			Dim uncompressedSize As Integer = fileSpecification.EmbeddedFile.UncompressedLength
			If uncompressedSize = 0 Then
				row.Cells(1).Value = "N/A"
				row.Cells(2).Value = GetDataSize(compressedSize)
			Else
				row.Cells(1).Value = GetDataSize(uncompressedSize)
				Dim compressionPercent As Double = (1 - CDbl(compressedSize) / uncompressedSize) * 100.0
				row.Cells(2).Value = String.Format("{0} ({1:F2}%)", GetDataSize(compressedSize), compressionPercent)
			End If
		End Sub

		Private Function GetDataSize(size As Integer) As String
			If size < 10000 Then
				Return String.Format("{0} Bytes", size)
			End If
			Return String.Format("{0} KB", Math.Round(size / 1024.0))
		End Function

		Private Sub buttonOk_Click(sender As Object, e As EventArgs)
			Close()
		End Sub

		Private Sub saveAsButton_Click(sender As Object, e As EventArgs)
			Dim fileSpecification As PdfEmbeddedFileSpecification = SelectedFileSpecification
			If fileSpecification IsNot Nothing Then
				saveFileDialog1.FileName = fileSpecification.Filename
				If saveFileDialog1.ShowDialog() = DialogResult.OK Then
					fileSpecification.EmbeddedFile.Save(saveFileDialog1.FileName)
				End If
			End If
		End Sub

		Private Sub embeddedFilesDataGridView_SelectionChanged(sender As Object, e As EventArgs)
			Dim fileSpecification As PdfEmbeddedFileSpecification = SelectedFileSpecification
			If fileSpecification IsNot Nothing Then
				_showingFileProperties = True
				compressionComboBox.SelectedItem = GetCompression(fileSpecification)
				desriptionTextBox.Text = fileSpecification.Description
				modifyDateLabel.Text = fileSpecification.EmbeddedFile.ModifyDate.ToString(DateTimeFormat)
				createDateLabel.Text = fileSpecification.EmbeddedFile.CreationDate.ToString(DateTimeFormat)
				_showingFileProperties = False
			End If
		End Sub

		Private Function GetCompression(fileSpecification As PdfEmbeddedFileSpecification) As PdfCompression
			Dim compression As PdfCompression = fileSpecification.EmbeddedFile.Compression
			If (compression And PdfCompression.Zip) <> 0 Then
				Return PdfCompression.Zip
			End If
			If (compression And PdfCompression.Lzw) <> 0 Then
				Return PdfCompression.Lzw
			End If
			If (compression And PdfCompression.RunLength) <> 0 Then
				Return PdfCompression.RunLength
			End If
			Return PdfCompression.None
		End Function

		Private Sub compressionComboBox_SelectedIndexChanged(sender As Object, e As EventArgs)
			If Not _showingFileProperties Then
				Dim fileSpecification As PdfEmbeddedFileSpecification = SelectedFileSpecification
				Try
					If fileSpecification IsNot Nothing Then
						fileSpecification.EmbeddedFile.Compression = DirectCast(compressionComboBox.SelectedItem, PdfCompression)
						UpdateRowInformation(embeddedFilesDataGridView.SelectedRows(0))
					End If
				Catch ex As Exception
					DemosTools.ShowErrorMessage(ex)
				End Try
			End If
		End Sub

		Private Sub desriptionTextBox_TextChanged(sender As Object, e As EventArgs)
			If Not _showingFileProperties Then
				Dim fileSpecification As PdfEmbeddedFileSpecification = SelectedFileSpecification
				If fileSpecification IsNot Nothing Then
					fileSpecification.Description = desriptionTextBox.Text
				End If
			End If
		End Sub

		Private Sub removeButton_Click(sender As Object, e As EventArgs)
			If embeddedFilesDataGridView.SelectedRows.Count > 0 Then
				Dim row As DataGridViewRow = embeddedFilesDataGridView.SelectedRows(0)
				Document.EmbeddedFiles.Remove(DirectCast(row.Tag, String))
				embeddedFilesDataGridView.Rows.Remove(row)
			End If
		End Sub

		Private Sub addButton_Click(sender As Object, e As EventArgs)
			If openFileDialog1.ShowDialog() = DialogResult.OK Then
				If Document.EmbeddedFiles Is Nothing Then
					Document.EmbeddedFiles = New PdfEmbeddedFileSpecificationDictionary(Document)
				End If
				controlButtonsPanel.Enabled = False
				controlButtonsPanel.Refresh()
				For Each filename As String In openFileDialog1.FileNames
					Try
						Text = String.Format(AddEmbeddedFileMessage, Path.GetFileName(filename))
						Dim embeddedFile As New PdfEmbeddedFile(Document, filename)
						Dim fileSpecification As New PdfEmbeddedFileSpecification(filename, embeddedFile)
						Dim name As String = Document.EmbeddedFiles.Add(fileSpecification)
						AddRow(name, fileSpecification)
						embeddedFilesDataGridView.Refresh()
					Catch ex As Exception
						MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.[Error])
					End Try
				Next
				Text = FormTitle
				controlButtonsPanel.Enabled = True
			End If
		End Sub

		#End Region

	End Class
End Namespace
